'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 技能
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var dataApi = require('../libs/dataApi');

var open_skill_map = {};
exports.getJobCanOpenSkillList = function (job_type, hero_lv) {
    var key = hero_lv * 100 + job_type;
    if (key in open_skill_map) {
        return open_skill_map[key];
    }

    var job_skill_type_conf = dataApi.hero_skill_type.findById(job_type);
    var skill_list = [];
    if (!job_skill_type_conf) {
        console.error('job type can not found:' + job_type);
        open_skill_map[key] = skill_list;
        return skill_list;
    }

    _.each(job_skill_type_conf, function (skill_type_conf) {
        var skill_lv_conf = _getSkillLvConf(skill_type_conf.skill_id, 1);
        if (!!skill_lv_conf &&
            skill_lv_conf.skill_openlvl <= hero_lv) {
            skill_list.push(skill_type_conf.skill_id);
        }
    });
    open_skill_map[key] = skill_list;
    return skill_list;
};

/**
 * 获取技能等级配置
 * @param  {[type]} skill_type [description]
 * @param  {[type]} skill_lv   [description]
 * @return {[type]}            [description]
 */
exports.getSkillLvConf = function (skill_type, skill_lv) {
    return _getSkillLvConf(skill_type, skill_lv);
};

var skill_buf_map = {};
/**
 * 获取技能附加属性
 * @param  {[type]} skill_type [description]
 * @param  {[type]} skill_lv   [description]
 * @return {[type]}            [description]
 */
exports.getSkillBufConf = function (skill_type, skill_lv) {
    var key = skill_lv * 100 + skill_type;
    if (key in skill_buf_map) {
        return skill_buf_map[key];
    }

    var skill_buff_all_conf = dataApi.add_skill_type.findById(skill_type);
    var add_skill_list = [];
    _.each(skill_buff_all_conf, function (skill_buf_conf) {
        if (skill_buf_conf.need_skillid_lvl <= skill_lv) {
            add_skill_list.push({
                add_skill_type: skill_buf_conf.type,
                skill_time: skill_buf_conf.skill_time,
                skill_range: skill_buf_conf.skill_range,
                value_1: skill_buf_conf.value_1,
                value_2: skill_buf_conf.value_2,
                value_3: skill_buf_conf.value_3
            });
        }
    });
    skill_buf_map[key] = add_skill_list;
    return add_skill_list;
};

/**
 * 获取技能的等级配置
 * @param  {[type]} skill_type [description]
 * @param  {[type]} skill_lv   [description]
 * @return {[type]}            [description]
 */
var _getSkillLvConf = function (skill_type, skill_lv) {
    var skill_conf = dataApi.hero_skill_lvl.findById(skill_type);
    if (!skill_conf) {
        console.error('skill type can not found:' + skill_type);
        return null;
    }
    for (var i = 0; i < skill_conf.length; i++) {
        var skill_lv_conf = skill_conf[i];
        if (skill_lv_conf.skill_lvl == skill_lv) {
            return skill_lv_conf;
        }
    }
    console.error('skill lv can not found:' + skill_lv);
    return null;
};
